<?php
/**
 * Created by PhpStorm.
 * User: 虚空之翼 <183700295@qq.com>
 * Date: 16/5/20
 * Time: 09:14
 */

namespace app\admin\controller;


use app\admin\model\WechatDepartment;
use app\admin\model\WechatUser;
use com\wechat\TPWechat;
use think\Config;

class Wechat extends Admin
{

    public function user() {
        $name = input('name');
        $map = ['nickname' => ['like', "%$name%"]];
        $list = $this->lists("WechatUser", $map);
        $department = WechatDepartment::column('group_id, name');

        foreach ($list as $key=>$value) {
            if (is_array($value['groupid'])){
                $departmentId = json_decode($value['groupid']);
                if($departmentId){
                    foreach ($departmentId as $k=>$v) {
                        $name = $department[$v];
                        if ($k < count($departmentId) - 1 ) {
                            $name .= ',';
                        }
                    }
                    $list[$key]['department'] = $name;
                }else{
                    $list[$key]['department'] = "暂无";
                }
            }else{
                $Depart = WechatDepartment::where('group_id',$value['groupid'])->find();
                $list[$key]['department'] = $Depart['name'];
            }
        }
        // 状态转化
        wechat_status_to_string($list);
        $this->assign('list',$list);
        $this->assign('department', $department);

        return $this->fetch();
    }
    /*
     * 获取  分组
     */
    /**
     * 同步通讯录用户
     */
    public function synchronizeUser() {
        $Wechat = new TPWechat(Config::get('party'));
        if($Wechat->errCode != 40001) {
            return $this->error("同步出错");
        }

        /* 获取关注者 */
        $list = $Wechat->getUserList();
        foreach($list['data']['openid'] as $value){
            $user = $Wechat->getUserInfo($value);
            $user['nickname'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u','',$user['nickname']);
            $res = WechatUser::where(['openid' => $user['openid']])->find();
            if ($res){
                if ($user['tagid_list']){
                    $user['tagid_list'] = json_encode($user['tagid_list']);
                }
                WechatUser::where(['openid' => $user['openid']])->update($user);
            }else{
                WechatUser::create($user);
            }
        }
        $data = "用户数:".count($list['data']['openid'])."!";
        return $this->success("同步成功", '', $data);
    }

    /**
     * 同步部门
     */
    public function synchronizeDp(){
        $Wechat = new TPWechat(Config::get('party'));
        if($Wechat->errCode != 40001) {
            return $this->error("同步出错");
        }

        $group = $Wechat->getGroup();
        foreach($group['groups'] as $value){
            if (WechatDepartment::where('group_id',$value['id'])->find()){
                WechatDepartment::where('group_id',$value['id'])->update(['name' => $value['name'],'count' => $value['count']]);
            }else{
                WechatDepartment::create(['name' => $value['name'],'count' => $value['count'],'group_id'=>$value['id']]);
            }
        }
        $data = "同步部门数:".count($group['groups'])."!";
        return $this->success("同步成功", '', $data);
    }
    public function department(){
        $list = $this->lists("WechatDepartment");
        $this->assign('list', $list);

        return $this->fetch();
    }
    /** 用户详情 */
    public function detail(){
        if(IS_POST){
            $data = input('post.');
            if (!empty($data['tel']) && $data['review_status'] == 0){
                $data['tel'] = '';
                $data['name'] = '';
                $data['fw_id'] = '';
                $data['score'] = 0;
                $data['order_id'] = 0;
            }
            $res = WechatUser::where('id',$data['id'])->update(['name' => $data['name'],'review_status' => $data['review_status'] , 'tel' => $data['tel'],'fw_id' => $data['fw_id'],'score' => $data['score'],'order_id' => $data['order_id']]);
            if($res){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }else{
            $id = input('id/d');
            $list = WechatUser::where('id',$id)->find();
            $this->assign('info',$list);
            return $this->fetch();
        }
    }
}